<?php
namespace App\Admin\Controllers\Traits\WxSetting2Traits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;

trait formDiyCodeTrait{

    public function index_diycode(Content $content)
    {
        return $content->title('Diy代码，每次更改需要重新下载前端生效')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_diycode()->render()}</div>
HTML
            );
    }
    protected function form_diycode(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->html(function () {
            return '';
        }, '<b>一、首页</b>');

        $form->htmlEditor('app_custom_page_index_position_1', '位置1：template部分')->help('<a href="https://img.mini.minisns.cn/2024/10/23/0/2fff5950b14725b31fd6ac8a82ec7d57.jpg" target="_blank">位置1</a>')->default(Settings::get('app_custom_page_index_position_1', ''));
        $form->htmlEditor('app_custom_page_index_scss', '首页css部分')->help('文件路径：/pages/tabbar/index/index.scss 全局css文件')->default(Settings::get('app_custom_page_index_scss', ''));

        $form->action('settings2/action/diycode');
        return $form;
    }

    public function diycode(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_custom_page_index_position_1', $request->input('app_custom_page_index_position_1'), true);
        Settings::set('app_custom_page_index_scss', $request->input('app_custom_page_index_scss'), true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);

        return $this->response()->success('成功！');
    }

}
